package com.greentravel.view;

import com.greentravel.model.User;
import com.greentravel.service.UserService;
import com.greentravel.service.impl.UserServiceImpl;
import com.greentravel.util.GUIUtil;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 注册界面
 * 实现用户注册功能
 */
public class RegisterView extends JFrame {

    private JTextField usernameField;
    private JPasswordField passwordField;
    private JPasswordField confirmPasswordField;
    private JTextField nameField;
    private JTextField contactField;
    private JTextField regionField;
    private JButton registerButton;
    private JButton backButton;

    private UserService userService;

    /**
     * 构造方法，初始化注册界面
     */
    public RegisterView() {
        userService = new UserServiceImpl();
        initComponents();
    }

    /**
     * 初始化界面组件
     */
    private void initComponents() {
        // 设置窗口基本属性
        setTitle("绿色出行倡导系统 - 用户注册");
        setSize(500, 400);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setResizable(false);
        GUIUtil.centerComponent(this);

        // 设置UI样式
        GUIUtil.setUIStyle();

        // 创建面板和布局
        JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
        mainPanel.setBorder(new EmptyBorder(20, 20, 20, 20));
        mainPanel.setBackground(Color.WHITE);

        // 标题面板
        JPanel titlePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        titlePanel.setBackground(Color.WHITE);
        JLabel titleLabel = GUIUtil.createLabel("用户注册", GUIUtil.TITLE_FONT);
        titleLabel.setForeground(GUIUtil.PRIMARY_COLOR);
        titlePanel.add(titleLabel);

        // 输入面板
        JPanel inputPanel = new JPanel(new GridLayout(6, 2, 10, 10));
        inputPanel.setBackground(Color.WHITE);

        JLabel usernameLabel = GUIUtil.createLabel("用户名:", GUIUtil.NORMAL_FONT);
        usernameField = new JTextField();
        usernameField.setFont(GUIUtil.NORMAL_FONT);

        JLabel passwordLabel = GUIUtil.createLabel("密码:", GUIUtil.NORMAL_FONT);
        passwordField = new JPasswordField();

        JLabel confirmPasswordLabel = GUIUtil.createLabel("确认密码:", GUIUtil.NORMAL_FONT);
        confirmPasswordField = new JPasswordField();

        JLabel nameLabel = GUIUtil.createLabel("姓名:", GUIUtil.NORMAL_FONT);
        nameField = new JTextField();
        nameField.setFont(GUIUtil.NORMAL_FONT);

        JLabel contactLabel = GUIUtil.createLabel("联系方式:", GUIUtil.NORMAL_FONT);
        contactField = new JTextField();
        contactField.setFont(GUIUtil.NORMAL_FONT);

        JLabel regionLabel = GUIUtil.createLabel("所在地区:", GUIUtil.NORMAL_FONT);
        regionField = new JTextField();
        regionField.setFont(GUIUtil.NORMAL_FONT);

        inputPanel.add(usernameLabel);
        inputPanel.add(usernameField);
        inputPanel.add(passwordLabel);
        inputPanel.add(passwordField);
        inputPanel.add(confirmPasswordLabel);
        inputPanel.add(confirmPasswordField);
        inputPanel.add(nameLabel);
        inputPanel.add(nameField);
        inputPanel.add(contactLabel);
        inputPanel.add(contactField);
        inputPanel.add(regionLabel);
        inputPanel.add(regionField);

        // 按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));
        buttonPanel.setBackground(Color.WHITE);

        registerButton = GUIUtil.createButton("注册");
        backButton = GUIUtil.createButton("返回登录");

        buttonPanel.add(registerButton);
        buttonPanel.add(backButton);

        // 将面板添加到主面板
        mainPanel.add(titlePanel, BorderLayout.NORTH);
        mainPanel.add(inputPanel, BorderLayout.CENTER);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);

        // 添加主面板到窗口
        setContentPane(mainPanel);

        // 添加按钮事件
        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                register();
            }
        });

        backButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                backToLogin();
            }
        });

        // 设置默认按钮
        getRootPane().setDefaultButton(registerButton);
    }

    /**
     * 处理注册业务逻辑
     */
    private void register() {
        // 获取用户输入
        String username = usernameField.getText().trim();
        String password = new String(passwordField.getPassword());
        String confirmPassword = new String(confirmPasswordField.getPassword());
        String name = nameField.getText().trim();
        String contact = contactField.getText().trim();
        String region = regionField.getText().trim();

        // 校验输入
        if (username.isEmpty() || password.isEmpty() || confirmPassword.isEmpty() ||
                name.isEmpty() || contact.isEmpty() || region.isEmpty()) {
            GUIUtil.showErrorMessage(this, "所有字段都不能为空");
            return;
        }

        if (!password.equals(confirmPassword)) {
            GUIUtil.showErrorMessage(this, "两次输入的密码不一致");
            passwordField.setText("");
            confirmPasswordField.setText("");
            return;
        }

        // 检查用户名是否已存在
        if (userService.isUsernameExist(username)) {
            GUIUtil.showErrorMessage(this, "用户名已存在，请选择其他用户名");
            return;
        }

        // 创建用户对象
        User user = new User(username, password, name, contact, region);

        // 调用服务层进行注册
        boolean success = userService.register(user);

        if (success) {
            // 注册成功
            GUIUtil.showInfoMessage(this, "注册成功，请登录");
            backToLogin();
        } else {
            // 注册失败
            GUIUtil.showErrorMessage(this, "注册失败，请稍后重试");
        }
    }

    /**
     * 返回登录界面
     */
    private void backToLogin() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                LoginView loginView = new LoginView();
                loginView.setVisible(true);
                dispose(); // 关闭注册窗口
            }
        });
    }
}